package com.aiju.cash.widgit;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiju.cash.R;

/**
 * @author: 佐为
 * @date: 2017-02-24 11:27
 * @version: V1.0
 * @Description: 会员卡片
 */
public class MemberCardVew extends RelativeLayout {

    private TextView nameTextView, discounTextView, timeTextView;

    public MemberCardVew(Context context) {
        super(context);
        initView(context);
    }

    public MemberCardVew(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_member_card, this);
        nameTextView = (TextView) findViewById(R.id.tv_card_name);
        timeTextView = (TextView) findViewById(R.id.tv_card_time);
        discounTextView = (TextView) findViewById(R.id.tv_card_discoun);
        SpannableString styledText = new SpannableString(discounTextView.getText());
        styledText.setSpan(new TextAppearanceSpan(context, R.style.style_big), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        styledText.setSpan(new TextAppearanceSpan(context, R.style.style1), 3, discounTextView.getText().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        discounTextView.setText(styledText, TextView.BufferType.SPANNABLE);
    }

    /**
     * 设置卡片名称
     *
     * @param name
     */
    public void setCardName(String name) {
        nameTextView.setText(name);
    }

    /**
     * 设置卡片折扣
     *
     * @param discount
     */
    public void setCardDiscount(String discount) {
        String dis = discount + "折";
        SpannableString styledText = new SpannableString(dis);
        styledText.setSpan(new TextAppearanceSpan(getContext(), R.style.style_big), 0, dis.indexOf("折"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        styledText.setSpan(new TextAppearanceSpan(getContext(), R.style.style1), dis.indexOf("折"), dis.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        discounTextView.setText(styledText, TextView.BufferType.SPANNABLE);
    }

    /**
     * 设置卡片有效时间
     *
     * @param time
     */
    public void setCardTime(String time) {
        timeTextView.setVisibility(VISIBLE);
        findViewById(R.id.tv_card_forever).setVisibility(GONE);
        timeTextView.setText(time);
    }

    /**
     * 显示无限期
     */
    public void showForever() {
        timeTextView.setVisibility(GONE);
        findViewById(R.id.tv_card_forever).setVisibility(VISIBLE);
    }
}
